package com.sugon.controller.module;

import com.sugon.annotation.AutoLog;
import com.sugon.entity.ModuleMaintainRecordEntity;
import com.sugon.service.ModuleMaintainRecordService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 模具维护记录单Controller
 *
 * @author sugon
 * @date 2019-08-09 11:28:49
 */
@Controller
@RequestMapping("modulemaintainrecord")
public class ModuleMaintainRecordController {
    @Autowired
    private ModuleMaintainRecordService moduleMaintainRecordService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<ModuleMaintainRecordEntity> moduleMaintainRecordList = moduleMaintainRecordService.queryList(query);
        int total = moduleMaintainRecordService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(moduleMaintainRecordList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("modulemaintainrecord:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        ModuleMaintainRecordEntity moduleMaintainRecord = moduleMaintainRecordService.queryObject(id);

        return R.ok().put("moduleMaintainRecord", moduleMaintainRecord);
    }

    /**
     * 保存
     */
    @AutoLog("模具维护记录单--保存")
    @RequestMapping("/save")
    @RequiresPermissions("modulemaintainrecord:save")
    @ResponseBody
    public R save(@RequestBody ModuleMaintainRecordEntity moduleMaintainRecord) {
        moduleMaintainRecordService.save(moduleMaintainRecord);

        return R.ok();
    }

    /**
     * 修改
     */
    @AutoLog("模具维护记录单--修改")
    @RequestMapping("/update")
    @RequiresPermissions("modulemaintainrecord:update")
    @ResponseBody
    public R update(@RequestBody ModuleMaintainRecordEntity moduleMaintainRecord) {
        moduleMaintainRecordService.update(moduleMaintainRecord);

        return R.ok();
    }
    /**
     * 修改
     */
    @AutoLog("模具维护记录单--更新记录")
    @RequestMapping("/updateRecord")
    @ResponseBody
    public R updateRecord(@RequestBody List<Map<String,Object>> recordList) {

        int num = moduleMaintainRecordService.updateStatus(recordList);
        if(num==recordList.size()){
            return R.ok();
        }else{
            return R.error("已经保存数量"+num+",需要保存的数量为"+recordList.size());
        }

    }

    /**
     * 删除
     */
    @AutoLog("模具维护记录单--删除")
    @RequestMapping("/delete")
    @RequiresPermissions("modulemaintainrecord:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        moduleMaintainRecordService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ModuleMaintainRecordEntity> list = moduleMaintainRecordService.queryList(params);

        return R.ok().put("list", list);
    }

}
